package main

import (
	"fmt"
	"internal/api/handler"
	"internal/store/mysql"
	"internal/store/redis"
	"internal/util"
	"os"
	"path/filepath"
	"runtime"
	"strings"
	"time"
)

func getProjectDir() string {
	if runtime.GOOS == "linux" {
		if err := os.Chdir("../"); err != nil {
			fmt.Println("Failed to retrieve the parent directory of the main goProject error,", err)
		}
	}
	dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
	if err != nil {
		fmt.Println("get goProject dir error,", err)
	}
	// 替换文件中\\改为/并且返回目录绝对路径
	return strings.Replace(dir, "\\", "/", -1)

}

func main() {
	// 初始化日志模块
	projectDir := getProjectDir()
	if err := util.InitLogger(projectDir); err != nil {
		fmt.Println("[logger]: Init logger error,", err)
	}

	// 初始化数据库模块
	mysql.InitMysqlConn()

	// 初始化redis模块
	redis.InitRedisCache()

	// 初始化消息总线程
	util.InitEventBus()

	// 开启协程启动gin服务
	go handler.InitHttpServer()

	// 主线程永不退出
	for {
		time.Sleep(3 * time.Second)
	}
}
